/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.PegawaiBean;

/**
 *
 * @author Jagadeesh
 */
public class DBClass {

    public Connection createConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ekud", "root", "hilman");
        return connection;
    }

    public List cekLogin(String username, String password) throws SQLException, ClassNotFoundException {

        Connection con = createConnection();

        PreparedStatement pstmt = con.prepareStatement("select PEGAWAI_ID, USERNAME, PASSWORD,ID_BAGIAN from pegawai where USERNAME=? AND PASSWORD=md5(?)");
        pstmt.setString(1, username);
        pstmt.setString(2, password);
        ResultSet rs = pstmt.executeQuery();
        List list = new ArrayList();
        PegawaiBean ubean = new PegawaiBean();

        if (rs.next()) {

            ubean.setPegawaiId(rs.getString(1));
            ubean.setNama(rs.getString(2));
            ubean.setIdBagian(rs.getString(4));
            list.add(ubean);
        }

        return list;
    }

    public void UpdateAccount(String username,String passBaru, String id) throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("UPDATE `pegawai` SET `USERNAME`=?,`PASSWORD`=md5(?) WHERE `PEGAWAI_ID`=?");
        //set values to prepared statement object by getting values from bean object
        pstmt.setString(1, username);
        pstmt.setString(2, passBaru);
       
        pstmt.setString(3, id);
        pstmt.executeUpdate();

    }
}
